﻿#include "Message.h"
#include"Sprite.h"
#include"Score.h"
#include"LifeIcon.h"
#include"BombIcon.h"
#include"BulletIcon.h"
#include"WeaponBank.h"
#include"ProtectionIcon.h"
#include<thread>
#include<qdebug.h>
Message::Message()
{
    CreateScore();
}
Message::Message(Sprite* player)
{
    CreateScore();
    setPlayer(player);
    int h = player->getWindow().height();
    m_LifeIcon = new LifeIcon(QPoint(0,h ));
    int x = m_LifeIcon->getSize().width() + m_LifeIcon->getTextWidth();
    m_BombIcon = new BombIcon(QPoint(x,h));
    x+= m_BombIcon->getSize().width() + m_BombIcon->getTextWidth();
    m_BulletIcon=new BulletIcon(QPoint(x, h));
    x+= m_BulletIcon->getSize().width() + m_BulletIcon->getTextWidth()+10;
   
    m_r = m_BombIcon->getSize().width() / 2;
    
    m_o = QPoint(player->getWindow().width() -m_r-5, h-m_r-5);
}
Message::~Message()
{
    if (m_Score != nullptr)
        delete m_Score;
    if (m_LifeIcon != nullptr)
        delete m_LifeIcon;
    if (m_BombIcon != nullptr)
        delete m_BombIcon;
    if (m_BulletIcon != nullptr)
        delete m_BulletIcon;
    if (m_ProtectionIcon != nullptr)
        delete m_ProtectionIcon;
}

void Message::update()
{
    if (m_LifeIcon != nullptr)
    {
        m_LifeIcon->update();
        m_LifeIcon->setNum(m_player->getHp());
    }
    if (m_BombIcon != nullptr)
    {
        m_BombIcon->update();
        m_BombIcon->setNum(m_player->getBombNum());
        qint32 time = m_player->getWeaponBank()->getGuidedMissileCdRemainingTime();
        //qDebug()<<time;
        if (time != -1)
        {
            m_BombIcon->ShowTime(true);
            //传送剩余cd的倒计时
            m_BombIcon->setTime(time/ 1000);
        }
        else
        {
            m_BombIcon->ShowTime(false);
        }
    }
    if (m_BulletIcon != nullptr)
    {
        int level = m_player->getWeaponBank()->getBulletLevel();
        m_BulletIcon->setNum(level);
        if (level == 1)
        {
            m_BulletIcon->ShowTime(false);
            
        }
        else
        {
            m_BulletIcon->ShowTime(true);
           //传送降级的倒计时
            m_BulletIcon->setTime(m_player->getWeaponBank()->getBulletLevelRemainingTime() / 1000);
        }
        m_BulletIcon->update();
    }
    if (m_ProtectionIcon == nullptr)
    {
        if (m_player->getProtectState())
        {
            m_ProtectionIcon = new ProtectionIcon(m_o,m_r,5);
        }
    }
    else if (!m_player->getProtectState())
    {
        auto temp = m_ProtectionIcon;
        m_ProtectionIcon = nullptr;
        delete temp;
    }
    else
    {
        m_ProtectionIcon->setTime(m_player->getProtectRemainingTime() / 1000);
    }
}

void Message::draw(QPainter* painter)
{
    
    //std::thread t1([=]() {if (m_ProtectionIcon != nullptr)m_ProtectionIcon->draw(painter); });
   // QPainter pain;
    if (m_ProtectionIcon != nullptr)
        m_ProtectionIcon->draw(painter);
    m_Score->draw(painter);
    if (m_LifeIcon != nullptr)
        m_LifeIcon->draw(painter);
    if (m_BombIcon != nullptr)
        m_BombIcon->draw(painter);
    if(m_BulletIcon!=nullptr)
        m_BulletIcon->draw(painter);
    //t1.join();
}

void Message::setPlayer(Sprite* player) 
{
    m_player = player;
    m_Score->setPlayer(player);
}

void Message::CreateScore()
{
    m_Score = new Score();
    QPen RectPen(Qt::GlobalColor::green);
    RectPen.setWidth(3);
    m_Score->setRectPen(RectPen);
    QPen NumPen(Qt::GlobalColor::red);
    m_Score->setNumTextPen(NumPen);
    //m_Score->setNumTextFont(QFont("Microsoft YaHei"));
}
